//
//  MySeatBezierView.m
//  Booking
//
//  Created by Maybe_文仔 on 2018/9/13.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "MySeatBezierView.h"

@implementation MySeatBezierView

- (void)drawRect:(CGRect)rect {
    if (!self.mySeatId) {
        return;
    }
    //设置填充颜色
    [KAPP_THEME_COLOR setFill];
    
    NSDictionary *areaListDic = [self readLocalFileWithName:@"SeatList"];
    NSArray *areaList = areaListDic[@"areas"];
    for (NSDictionary *dict in areaList) {
        
        if ([dict[@"roomId"] isEqualToString:self.mySeatId]) {
            //创建UIBezierPath
            UIBezierPath *apath = ({
                
                UIBezierPath *path = [UIBezierPath bezierPath];
                path.lineWidth     = 0.0f;              //设置线条宽度
                path.lineCapStyle  = kCGLineCapRound;   //设置拐角
                path.lineJoinStyle = kCGLineCapRound;  //终点处理
                
                NSArray *areaArr = dict[@"area"];
                for (int i = 0; i < areaArr.count; i++) {
                    NSDictionary *dic = areaArr[i];
                    if (i == 0) {
                        //设置起始点
                        [path moveToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                        
                    }
                    else{
                        //增加线条
                        [path addLineToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                    }
                }
                //关闭路径
                [path closePath];
                
                path;
            });
            
            //根据坐标连线
            [apath stroke];
            //根据闭合路线填充色彩
            [apath fill];
        }
    }
    
}

-(void)setMySeatId:(NSString *)mySeatId{
    _mySeatId = mySeatId;
    [self setNeedsDisplay];
}

//读取本地json测试文件
- (NSDictionary *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}


@end
